package com.newlink.building.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RadioButton
import androidx.recyclerview.widget.RecyclerView
import com.newlink.building.R
import org.linphone.core.AudioDevice

data class OptionItem(
    val iconRes: Int,
    val iconResSelect: Int,
    val text: String,
    val type: AudioDevice.Type
)

class SingleChoiceAdapter(
    private val options: List<OptionItem>,
    private val currentType: AudioDevice.Type,
    private var selectedPosition: Int = -1,
    private val onOptionSelected: (OptionItem) -> Unit
) : RecyclerView.Adapter<SingleChoiceAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val radioButton: RadioButton = view.findViewById(R.id.radio_button)
        val radioIcon: ImageView = view.findViewById(R.id.radio_icon)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_radio_button_with_icon, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val option = options[position]
        holder.radioButton.text = option.text
        if (option.type == currentType) {

        }
        holder.radioIcon.setImageResource(
            if (option.type == currentType) {
                option.iconResSelect
            } else {
                option.iconRes
            }
        )
//        holder.radioIcon.setImageResource(option.iconRes)
        holder.radioButton.isChecked = position == selectedPosition

        holder.radioButton.setOnClickListener {
            selectedPosition = holder.adapterPosition
            notifyDataSetChanged()
            onOptionSelected(option)
        }

        holder.radioIcon.setOnClickListener {
            holder.radioButton.performClick() // 点击图标时触发 RadioButton 点击事件
        }
    }

    override fun getItemCount(): Int = options.size
}